package com.ihelin.insurance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ihelin.insurance.entity.AnswerEntity;
import com.ihelin.insurance.entity.FeedbackEntity;
import com.ihelin.insurance.entity.QuestionEntity;
import com.ihelin.insurance.entity.SalesmanEntity;
import com.ihelin.insurance.service.AnswerService;
import com.ihelin.insurance.service.FeedbackService;
import com.ihelin.insurance.service.QuestionService;
import com.ihelin.insurance.service.SalesmanService;
import com.ihelin.insurance.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author iHelin
 * @since 2021/2/28 15:04
 */
@RestController
public class SalesController {

    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;

    /**
     * 营销人员注册
     *
     * @param salesmanEntity
     * @return
     */
    @PostMapping("/sales/register")
    public R register(@RequestBody SalesmanEntity salesmanEntity) {
        String username = salesmanEntity.getUsername();
        List<SalesmanEntity> agentList = salesmanService.list(new QueryWrapper<SalesmanEntity>().eq("username", username));
        if (agentList != null && agentList.size() > 0) {
            return R.error("用户名已存在！");
        }
        salesmanService.save(salesmanEntity);
        return R.ok();
    }

    /**
     * 营销人员登录
     *
     * @param salesmanEntity
     * @param session
     * @return
     */
    @PostMapping("/sales/login")
    public R login(@RequestBody SalesmanEntity salesmanEntity, HttpSession session) {
        String username = salesmanEntity.getUsername();
        SalesmanEntity userFromDB = salesmanService.getOne(new QueryWrapper<SalesmanEntity>().eq("username", username));
        if (userFromDB != null && userFromDB.getPassword().equals(salesmanEntity.getPassword())) {
            session.setAttribute("salesUser", userFromDB);
            return R.ok();
        }
        return R.error("用户名或密码错误！");
    }

    @GetMapping("/sales/feedback/list")
    public R feedbacks(Long page, Long limit, String key) {
        QueryWrapper<FeedbackEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(key)) {
            queryWrapper.eq("agent_id", key);
        }
        IPage<FeedbackEntity> data = feedbackService.page(new Page<>(page, limit), queryWrapper);
        return R.ok(data);
    }

    /**
     * 信息
     */
    @GetMapping("/sales/feedback/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        FeedbackEntity feedback = feedbackService.getById(id);
        return R.ok(feedback);
    }

    /**
     * 新增
     */
    @PostMapping("/sales/feedback/save")
    public R save(@RequestBody FeedbackEntity feedback) {
        feedbackService.save(feedback);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/sales/feedback/update")
    public R update(@RequestBody FeedbackEntity feedback) {
        feedbackService.updateById(feedback);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/sales/feedback/delete")
    public R delete(@RequestBody List<Integer> ids) {
        feedbackService.removeByIds(ids);
        return R.ok();
    }

    /**
     * Question列表
     */
    @GetMapping("/sales/question/list")
    public R questions(Long page, Long limit, String key) {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(key)) {
            queryWrapper.eq("id", key);
        }
        IPage<QuestionEntity> data = questionService.page(new Page<>(page, limit), queryWrapper);
        return R.ok(data);
    }


    /**
     * Question信息
     */
    @GetMapping("/sales/question/info/{id}")
    public R questionInfo(@PathVariable("id") Integer id) {
        QuestionEntity question = questionService.getById(id);
        return R.ok().put("data", question);
    }

    /**
     * 新增Question
     */
    @PostMapping("/sales/question/save")
    public R saveQuestion(@RequestBody QuestionEntity question, HttpSession session) {
        SalesmanEntity salesUser = (SalesmanEntity) session.getAttribute("salesUser");
        question.setSalesId(salesUser.getId());
        question.setSalesName(salesUser.getName());
        questionService.save(question);
        return R.ok();
    }

    /**
     * 修改Question
     */
    @PutMapping("/sales/question/update")
    public R updateQuestion(@RequestBody QuestionEntity question) {
        questionService.updateById(question);
        return R.ok();
    }

    /**
     * 删除Question
     */
    @DeleteMapping("/sales/question/delete")
    public R deleteQuestion(@RequestBody List<Integer> ids) {
        questionService.removeByIds(ids);
        return R.ok();
    }

    @GetMapping("/sales/answer/list")
    public R answers(Long page, Long limit, String key) {
        QueryWrapper<AnswerEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(key)) {
            queryWrapper.eq("id", key);
        }
        IPage<AnswerEntity> data = answerService.page(new Page<>(page, limit), queryWrapper);
        return R.ok(data);
    }

    /**
     * 评分
     *
     * @param answerEntity
     * @param session
     * @return
     */
    @PostMapping("/sales/answer/score")
    public R addScore(@RequestBody AnswerEntity answerEntity, HttpSession session) {
        SalesmanEntity salesUser = (SalesmanEntity) session.getAttribute("salesUser");
        answerEntity.setSalesId(salesUser.getId());
        answerEntity.setSalesName(salesUser.getName());
        answerService.updateById(answerEntity);
        return R.ok();
    }

    /**
     * Answer 信息
     */
    @GetMapping("/sales/answer/info/{id}")
    public R answerInfo(@PathVariable("id") Integer id) {
        AnswerEntity answerEntity = answerService.getById(id);
        return R.ok(answerEntity);
    }


}
